package com.piece.core.framework.support.convert;

import com.piece.core.framework.util.string.StringUtil;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 汉字辅助类
 */
@Slf4j
public class PinyinConvert {

    /**
     * 汉字转换为全拼
     */
    public static final String toPinYin(String src) {
        if (null == src) {
            return "";
        }

        char[] t1 = src.toCharArray();
        String[] t2 = new String[t1.length];

        // 设置汉字拼音输出的格式
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;

        try {
            for (int i = 0; i < t0; i++) {
                // 判断是否为汉字字符
                if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    // 将汉字的几种全拼都存到t2数组中
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    // 取出该汉字全拼的第一种读音并连接到字符串t4后
                    t4 += t2[0];
                } else {
                    // 如果不是汉字字符，直接取出字符并连接到字符串t4后
                    t4 += Character.toString(t1[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            log.error("汉字转拼音异常：{}", e.getMessage());
        }
        return StringUtil.isEmpty(t4) ? src : t4;
    }

    /**
     * 汉字转换为全拼，拼音首字母大写
     */
    public static final String toCamelPinYin(String src) {
        char[] t1 = src.toCharArray();
        String[] t2 = new String[t1.length];

        // 设置汉字拼音输出的格式
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "", t = "";
        int t0 = t1.length;

        try {
            for (int i = 0; i < t0; i++) {
                // 判断是否为汉字字符
                if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    // 将汉字的几种全拼都存到t2数组中
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    // 取出该汉字全拼的第一种读音
                    t = t2[0];
                } else {
                    // 如果不是汉字字符，直接取出字符
                    t = Character.toString(t1[i]);
                }
                t = t.substring(0, 1).toUpperCase() + t.substring(1);
                t4 += t;
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            log.error("汉字转拼音异常：{}", e.getMessage());
        }
        return StringUtil.isEmpty(t4) ? src : t4;
    }

    /**
     * 提取每个汉字的首字母
     */
    public static final String toHeaderPinYin(String str) {
        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            // 提取汉字的首字母
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (null != pinyinArray) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return StringUtil.isEmpty(convert) ? str : convert;
    }

    /**
     * 提取每个汉字的大写首字母
     */
    public static final String toUpperHeaderPinYin(String str) {
        return toHeaderPinYin(str).toUpperCase();
    }

    /**
     * 按汉字首字母排序
     */
    public static final List sort(List<String> words) {
        return words.stream().sorted(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                String pinyin_1 = toHeaderPinYin(o1);
                String pinyin_2 = toHeaderPinYin(o2);
                return pinyin_1.compareTo(pinyin_2);
            }
        }).collect(Collectors.toList());
    }
}
